package com.sh.weblogspringboot.admin.schedule;

import com.sh.weblogspringboot.common.domain.dos.StatisticsArticlePVDO;
import com.sh.weblogspringboot.common.domain.mapper.StatisticsArticlePVMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author songHai
 * @Classname InitPVRecordScheduledTask
 * @Description
 * @Date 2025/4/13 13:34
 */
@Component
@Slf4j
public class InitPVRecordScheduledTask {

    @Autowired
    private StatisticsArticlePVMapper articlePVMapper;

    /**
     * 每天晚间23点执行
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void execute() {
        // 定时任务执行的业务逻辑
        log.info("==>开始执行初始化明日PV访问量记录定时任务");
        // 当日日期
        LocalDate currentDate = LocalDate.now();
        // 明日
        LocalDate tomorrowDate = currentDate.plusDays(1);
        // 组装插入记录
        StatisticsArticlePVDO articlePVDO = StatisticsArticlePVDO.builder()
                .pvDate(tomorrowDate)
                .pvCount(0L)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();

        articlePVMapper.insert(articlePVDO);
        log.info("==> 结束执行初始化明日PV访问量记录定时任务");
    }
}
